﻿using CollegeRevApp.DataModels;
using CollegeRevApp.Web.DataAdapters;
using CollegeRevApp.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace CollegeRevApp.Web.Controllers
{
    /// <summary>
    /// API controller for College
    /// </summary>
    public class CollegeController : ApiController
    {

        ICollegeAppAdapter _adapter = new CollegeAppAdapter();

        // GET crapp/college
        public IHttpActionResult Get()
        {
            CollegeIndexVM vm = new CollegeIndexVM();
            vm.Colleges = _adapter.GetAllColleges();

            foreach (var college in vm.Colleges)
            {
                college.AverageRating = _adapter.GetAverageRating(college.Id);
            }

            return Ok(vm);
        }

        // GET crapp/college/id
        public IHttpActionResult Get(int id)
        {
            CollegeDetailVM vm = new CollegeDetailVM();

            vm.College = _adapter.GetCollege(id);
            vm.Reviews = _adapter.GetReviewsForCollege(id);

            return Ok(vm);
        }

        // POST crapp/college
        public IHttpActionResult Post([FromBody]College college)
        {
            college = _adapter.AddCollege(college);
            return Ok(college);
        }

        // PUT crapp/college
        public IHttpActionResult Put(int id, [FromBody]College college)
        {
            college = _adapter.EditCollege(id, college);
            return Ok(college);
        }

        // DELETE crapp/college/id
        public IHttpActionResult Delete(int id)
        {
            College college = _adapter.DeleteCollege(id);
            return Ok();
        }

    }
}
